﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CommonUtils;
using TERP.WaterClient.Entities.Auth;
using TERP.WaterClient.Common;

namespace TERP.WaterClient.DbServices
{
	public class GroupService: BaseService
	{
		public GroupService()
			: base()
		{
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lstGrpCode"></param>
        /// <param name="dsMapGrpUser"></param>
        public void MapObjectToGroup(List<string> lstGrpCode, DataSet dsMapGrpObj)
        {
            string strTId = SystemGlobal.Instance.GetNextTid();
            //
            try
            {
                dsMapGrpObj.Tables[0].TableName = DbTable.Tbl_Sys_MapGroupObject;
                DataSet mdsFinal = _dbService.Ser_SysSaveMapSysGroupSysObject(
                    _strGwUserCode, _strGwPassword,
                    strTId, _strSessionId,
                    Util.StretchListString(lstGrpCode),
                    dsMapGrpObj
                );
                //
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
                if (CMyDataSet.HasError(mdsFinal))
                {
                    throw GenServiceException(mdsFinal);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


		/// <summary>
		/// 
		/// </summary>
		/// <param name="lstGrpCode"></param>
		/// <param name="dsMapGrpUser"></param>
		public void MapUserToGroup(List<string> lstGrpCode,DataSet dsMapGrpUser){
			string strTId = SystemGlobal.Instance.GetNextTid();
			//
			try
			{
                dsMapGrpUser.Tables[0].TableName = DbTable.Tbl_Sys_MapUserGroup;
				DataSet mdsFinal = _dbService.Ser_SysSaveMapSysGroupSysUser(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId,
					Util.StretchListString(lstGrpCode),
					dsMapGrpUser		
				);
				//
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
				if (CMyDataSet.HasError(mdsFinal))
				{
					throw GenServiceException(mdsFinal);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="grpCollection"></param>
		public void SaveGroup(List<string> lstGroupCodeListForDelete,DataSet dsGroup){
			string strTId = SystemGlobal.Instance.GetNextTid();
            string strGroupCodeListForDelete = string.Empty;
            if (lstGroupCodeListForDelete.Count>0)
            {
                strGroupCodeListForDelete=Util.StretchListString(lstGroupCodeListForDelete);
            }
			//
			try
			{
				DataSet mdsFinal = _dbService.Ser_SysSaveGroup(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId,
                   strGroupCodeListForDelete,
					dsGroup
				);
				//
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
				if (CMyDataSet.HasError(mdsFinal))
				{
					throw GenServiceException(mdsFinal);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public FunctionCollection GetFunction()
        {
            try
            {
                string strTId = SystemGlobal.Instance.GetNextTid();
                DataSet mdsFinalGrp = _dbService.Ser_SysGetObject(
                    _strGwUserCode,
                    _strGwPassword,
                    strTId, _strSessionId
                    , ""			//partner code list
                    , ""			//obj code pattern
                    , Constants.Flag.Active            //flag active list
                );
                mdsFinalGrp = TERP.Utils.CUtils.MyDSDecode(mdsFinalGrp);
                if (CMyDataSet.HasError(mdsFinalGrp))
                {
                    throw GenServiceException(mdsFinalGrp);
                }
                //
                FunctionCollection fncCollection = new FunctionCollection();
                fncCollection.DataTableFunction = mdsFinalGrp.Tables[DbTable.Tbl_Function_Object];
                return fncCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


		/// <summary>
		/// 
		/// </summary>
		public GroupCollection GetGroup()
		{
			string tlbMapGrpUsr = "Map_SG_SU";
			string tblMapGrpObj = "Map_SG_SO";
			//
			try
			{
				string strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalGrp = _dbService.Ser_SysGetGroup(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					,""			//group code list
					,""			//group code pattern
				);
				//
                mdsFinalGrp = TERP.Utils.CUtils.MyDSDecode(mdsFinalGrp);
				if (CMyDataSet.HasError(mdsFinalGrp))
				{
					throw GenServiceException(mdsFinalGrp);
				}
				//
				strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalUsr = _dbService.Ser_SysGetMapSysGroupSysUser(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					, ""			//group code list
					, ""			//user code list
					, Constants.Flag.Active			//flag active list User
				);
				//
                mdsFinalUsr = TERP.Utils.CUtils.MyDSDecode(mdsFinalUsr);
				if (CMyDataSet.HasError(mdsFinalUsr))
				{
					throw GenServiceException(mdsFinalGrp);
				}
				//
				strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalObj = _dbService.Ser_SysGetMapSysGroupSysObject(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					, ""			//group code list
					, ""			//partner code list
					, ""			//obj code list
					, ""			//obj type list
					, Constants.Flag.Active			//flag active list object
				);
				//
                mdsFinalObj = TERP.Utils.CUtils.MyDSDecode(mdsFinalObj);
				if (CMyDataSet.HasError(mdsFinalObj))
				{
					throw GenServiceException(mdsFinalObj);
				}
				GroupCollection grpCollection = new GroupCollection();
				//
				DataTable dtbGrp = mdsFinalGrp.Tables[DbTable.Tbl_Group];
				DataTable dtbMapGrpUser = mdsFinalUsr.Tables[tlbMapGrpUsr];
				DataTable dtbGrpObj = mdsFinalObj.Tables[tblMapGrpObj];
				//
				foreach (DataRow dtRowGrp in dtbGrp.Rows)
				{
					Group grp = new Group();
					grp.GroupCode = Convert.ToString(dtRowGrp[TblGroup.Group_Code]);
					grp.GroupName = Convert.ToString(dtRowGrp[TblGroup.Group_Name]);
					//query user belong
					string tmpGrpCode = Convert.ToString(dtRowGrp[TblGroup.Group_Code]);
					DataRow[] arrRowUser = dtbMapGrpUser.Select(String.Format(TblGroup.Group_Code + "='{0}'", tmpGrpCode));
					string prefixSu = "SU";
					//
					UserCollection userCollection = new UserCollection();
					foreach (DataRow dtRowUser in arrRowUser)
					{
						User user = new User();
						user.UserCode = Convert.ToString(dtRowUser[TblUser.User_Code]);
						user.UserName = Convert.ToString(dtRowUser[TblUser.User_Name]);
						user.UserPassword = Convert.ToString(dtRowUser[TblUser.User_Password]);
						user.FlagActive = Convert.ToString(dtRowUser[prefixSu + TblUser.Flag_Active]);
                      
						//
						userCollection.Add(user);
					}
					userCollection.FillToDataTable();
					//////////////////////////////////////////////////////////////////////////
					//query obj belong
					DataRow[] arrRowObj = dtbGrpObj.Select(String.Format(TblGroup.Group_Code + "='{0}'", tmpGrpCode));
					string prefixSO = "SO";
					//
					FunctionCollection objCollection = new FunctionCollection();
					foreach (DataRow dtRowObj in arrRowObj)
					{
						FunctionObject functionObj = new FunctionObject();
						functionObj.ObjectCode = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code]);
						functionObj.ObjectName = Convert.ToString(dtRowObj[TblFunctionObject.Object_Name]);                       
						functionObj.ObjectType = Convert.ToString(dtRowObj[TblFunctionObject.Object_Type]);
						functionObj.PartnerCode = Convert.ToString(dtRowObj[TblFunctionObject.Partner_Code]);
						functionObj.FlagActive = Convert.ToString(dtRowObj[prefixSO+TblFunctionObject.Flag_Active]);
						//
						objCollection.Add(functionObj);
					}
					objCollection.FillToDataTable();
					///
					grp.ListUserBelongTo = userCollection;
					grp.ListFunction = objCollection;
					grpCollection.Add(grp);
				}
				grpCollection.FillToDataTable();
				return grpCollection;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
